<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加课程</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">课程名称</label>
        <div class="layui-input-block">
            <input type="text" name="courseName" lay-verify="required" lay-reqtext="课程名称不能为空" placeholder="请输入课程名称" value="" class="layui-input">
            <tip>填写新课程的名称。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">教练id</label>
        <div class="layui-input-block">
            <input type="text" name="coachId" lay-verify="required" lay-reqtext="教练id不能为空" placeholder="请输入教练id" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">课时</label>
        <div class="layui-input-block">
            <input type="text" name="courseHour" lay-verify="required" lay-reqtext="课时不能为空" placeholder="请输入课时" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">课程功能</label>
        <div class="layui-input-block">
            <input type="text" name="courseFunction" placeholder="请输入课程功能" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">课程分类id</label>
        <div class="layui-input-block">
            <input type="text" name="coursesortId" lay-verify="required" lay-reqtext="课程分类id不能为空" placeholder="请输入课程分类id" value="" class="layui-input">
            <!--<select name="coursesortId" id="coursesortId">-->
            <!--    <option value=""></option>-->
            <!--    <option value=""></option>-->
            <!--    <option value=""></option>-->
            <!--    <option value=""></option>-->
            <!--</select>-->
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">课程图片</label>
        <div class="layui-input-block">
            <input type="file"  id="file" name="uploadFile" onchange="upFile()" autocomplete="off"
                   class="layui-btn" width="100px">
            <input hidden="hidden" name="courseImg">
        </div>
        <div class="layui-input-inline">
            <img src="" width="50px" height="50px" id="courseImg" style="display: none">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">课程价格</label>
        <div class="layui-input-block">
            <input type="number" name="coursePrice" lay-verify="required" lay-reqtext="课程价格不能为空" placeholder="请输入课程价格" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">课程介绍</label>
        <div class="layui-input-block">
            <textarea name="courseText" class="layui-textarea" placeholder="请输入课程介绍"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>

    function upFile() {
        var $ = layui.$;
        //获取上传的文件
        var file = $("#file")[0].files[0];
        var myForm = new FormData();//必须使用FormData表达上传数据
        myForm.append("file", file);
        $.ajax({
            method: "post",
            url: "/courseImgUpload",
            data: myForm,
            processData: false,//禁止类型转换//必须false才会避开jQuery对 formdata 的默认处理
            contentType: false,//必须false才会自动加上正确的Content-Type
            dataType: "text",
            mimeType: "multipart/form-data",
            success: function (data) {
                console.log(data);
                //设置动态框中img标签中的src
                $('#courseImg').attr('src', '/courseImg/' + data);
                //将图片名称变为data
                $('input[name=courseImg]').val(data);
            },
            error: function (err) {
                console.log("error：" + err);
            }
        });
    }

    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            console.log(data);
            $.ajax({
                url: "/addCourse",
                method:'post',
                data: data.field,
                dataType: "json",
                success: function (data) {
                    if (data.save) {
                        //成功后弹出提示，刷新表格
                        layer.msg(data.data);
                        parent.layui.table.reload('currentTableId');
                        var Index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(Index);
                    } else {
                        layer.msg(data.data);
                        parent.layui.table.reload('currentTableId');
                        var Index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(Index);
                    }
                },
                error: function () {
                    layer.msg("添加失败")
                    parent.layui.table.reload('currentTableId');
                    var Index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(Index);
                }
            });
            return false;
        });
    });
</script>
</body>
</html>